package com.base.commom;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.base.commom.http.RequestUtil;


/**
 * ElasticSearch 全文检索工具
 * @author chenluqiang
 * @date 2018年11月29日 下午8:32:56
 */
public final class ESUtils {
	private static String HOST = "http://192.168.99.100:9200";
	private static String result = null;
	private static final String ACKNOWLEDGED = "acknowledged";

	/**
	 * 添加索引
	 * @date 2018年11月29日 下午8:49:08
	 * @param url 请求地址
	 * @param indexs 多索引名称
	 * @return
	 */
	public static boolean addIndex(String url,String ...indexs) {
		
		try {
			if(indexs == null || indexs.length == 0) {
				return false;
			}else {
				url = urlPathConcat(url, indexs);
			}
			//添加成功返回的json格式
			//{"acknowledged":true,"shards_acknowledged":true,"index":"goods"}
			String content = RequestUtil.httpClientPut(url, null, false).getContent();
			result = content;
			Map<String, Object> map = (Map<String, Object>) JSON.parseObject(content);
			Object mapObject = map.get(ACKNOWLEDGED);
			if(mapObject == null) {
				return false;
			}
			return Boolean.parseBoolean(mapObject.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 删除索引
	 * @date 2018年11月29日 下午8:49:08
	 * @param url 请求地址
	 * @param indexs 多索引名称
	 * @return
	 */
	public static boolean deleteIndex(String url,String ...indexs) {
		
		try {
			if(indexs == null || indexs.length == 0) {
				return false;
			}else {
				url = urlPathConcat(url, indexs);
			}
			//删除成功返回的json格式
			//{"acknowledged":true}
			String content = RequestUtil.httpClientDelete(url, null).getContent();
			result = content;
			Map<String, Object> map = (Map<String, Object>) JSON.parseObject(content);
			Object mapObject = map.get(ACKNOWLEDGED);
			if(mapObject == null) {
				return false;
			}
			return Boolean.parseBoolean(mapObject.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	//添加
	public static boolean addData(String url,Map<String, String> data,String ...indexs) {
		
		try {
			if(indexs == null || indexs.length == 0) {
				return false;
			}else {
				url = urlPathConcat(url, indexs);
			}
			String content = RequestUtil.httpClientPost(url, data, true).getContent();
			result = content;
			Map<String, Object> map = (Map<String, Object>) JSON.parseObject(content);
			Object mapObject = map.get(ACKNOWLEDGED);
			if(mapObject == null) {
				return false;
			}
			return Boolean.parseBoolean(mapObject.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	//获取数据
	public static  boolean getData(String url,String ... indexs){
		try {
			if(indexs == null || indexs.length == 0) {
				return false;
			}else {
				url = urlPathConcat(url, indexs);
			}
			String content = RequestUtil.httpClientGet(url, null, null).getContent();
			result = content;
			Map<String, Object> map = (Map<String, Object>) JSON.parseObject(content);
			Object mapObject = map.get(ACKNOWLEDGED);
			if(mapObject == null) {
				return false;
			}
			return Boolean.parseBoolean(mapObject.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * url路径拼接
	 * @date 2018年11月29日 下午9:02:37
	 * @param url
	 * @param indexs
	 * @return
	 */
	private static String urlPathConcat(String url, String... indexs) {
		for (String index : indexs) {
			boolean startsWith = index.startsWith("/");
			if(!startsWith) {
				url += ("/" + index);
			}else {
				url += index;
			}
			
		}
		return url;
	}
	/**
	 * 获取结果集数据
	 * 
	 * @author chenluqiang
	 * @date 2018年11月29日 下午8:53:50
	 * @return
	 */
	public static String getResult() {
		return ESUtils.result;
	}
	
	public static void main(String[] args) {
		//添加索引
//		boolean index = addIndex(HOST, "goods");
		//添加数据
		/*Map<String, String> data = new HashMap<String, String>();
		data.put("name", "张三");
		data.put("title", "大品牌手机");
		data.put("desc", "大品牌手机福建省客服就是了束带结发乐山大佛卡萨丁撒");
		data.put("id","1");
//		http://192.168.99.100:9200/goods
		boolean index = addData(HOST,data, "goods/tyy");*/

		//获取数据
		boolean index = getData(HOST,"goods/type/AWo9yI2L3jZpNrbjiK2z");
		System.out.println(index);
		System.out.println(ESUtils.result);
	}
}
